package com.fbty.mt.utils;

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;

import com.fbty.mt.domain.IpInfo;

public class BatchSpeed {

	public static List<IpInfo> getSpeed(List<IpInfo> list) {
		final List<IpInfo> ips = new ArrayList<IpInfo>();
		final Deque<IpInfo> deque = new LinkedBlockingDeque<IpInfo>();
		deque.addAll(list);
		over: for (int i = 0; i < 20; i++) {
			if (deque.size() == 0)
				break over;
			if (deque.size() > 0) {
				new Thread() {
					public void run() {
						if (deque.size() > 0) {
							IpInfo ip = deque.removeFirst();
							String value = IPSpeed.getSpeed(ip.getIp().trim());
							if ("NaN".equals(value.trim())) {
								value = "--";
							}
							ip.setSpeed(value);
							Thread.currentThread().setPriority(MAX_PRIORITY);
							ips.add(ip);
						}
					}
				}.start();
			}else{
				break;
			}
			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		while(true){
			if(list.size()==ips.size()){
				return ips;
			}
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
